package com.parking.carsystem.parkingchargesystem.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.activity.CouponActivity;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.CouponNotUseModel;
import com.parking.carsystem.parkingchargesystem.utils.TimeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;

public class CouponFailureFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.coupon_notuse_data)
    RecyclerView couponNotuseData;
    @BindView(R.id.not_data)
    ImageView notData;
    private List<CouponNotUseModel.RowsBean> couponRows;
    private EXTRecyclerAdapter<CouponNotUseModel.RowsBean> recyclerAdapter;
    private CouponActivity context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_coupon_notuse, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initView();
        initData();
        return inflate;
    }

    private void initData() {
        OkGo.<String>get(Constant.url + Constant.LISTUSEINFO)
                .params("status", 1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        CouponNotUseModel couponNotUseModel = gson.fromJson(response.body(), CouponNotUseModel.class);
                        if (couponNotUseModel.code == 200 && couponNotUseModel.rows != null && couponNotUseModel.rows.size() > 0) {
                            couponRows = couponNotUseModel.rows;
                            recyclerAdapter.setDatas(couponRows);
                            recyclerAdapter.notifyDataSetChanged();
                            notData.setVisibility(View.GONE);
                            context.setFailureText(couponNotUseModel.rows.size());
                        } else {
                            context.setFailureText(0);
                            notData.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        context.setFailureText(0);
                    }
                });
    }

    private void initView() {
        context = (CouponActivity) getActivity();
        couponNotuseData.setLayoutManager(new LinearLayoutManager(context));
        recyclerAdapter = new EXTRecyclerAdapter<CouponNotUseModel.RowsBean>(R.layout.item_coupon_free_status) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, CouponNotUseModel.RowsBean rowsBean) {
                int type = rowsBean.coupon.couponRule.type;
                TextView coupon_time = extViewHolder.findViewById(R.id.coupon_time);
                ImageView image_status = extViewHolder.findViewById(R.id.image_status);
                RelativeLayout not_use_data = extViewHolder.findViewById(R.id.not_use_data);
                TextView coupon_time_text = extViewHolder.findViewById(R.id.coupon_time_text);
                TextView item_coupon_message = extViewHolder.findViewById(R.id.item_coupon_message);
                TextView be_overdue_time = extViewHolder.findViewById(R.id.be_overdue_time);
                TextView due_time = extViewHolder.findViewById(R.id.due_time);
                TextView use_message = extViewHolder.findViewById(R.id.use_message);
                image_status.setVisibility(View.VISIBLE);
                image_status.setImageResource(R.mipmap.failure);
                not_use_data.setVisibility(View.GONE);
                coupon_time.setTextColor(getResources().getColor(R.color.coupon_text_color));
                coupon_time_text.setTextColor(getResources().getColor(R.color.coupon_text_color));
                item_coupon_message.setTextColor(getResources().getColor(R.color.coupon_text_color));
                due_time.setTextColor(getResources().getColor(R.color.coupon_text_color));

                if (type == 1) {
                    coupon_time_text.setVisibility(View.INVISIBLE);
                    coupon_time.setText("免");
                } else if (type == 2) {
                    coupon_time_text.setVisibility(View.VISIBLE);
                    coupon_time_text.setText("元");
                    coupon_time.setText(String.valueOf(rowsBean.coupon.couponRule.deductionFee));
                } else if (type == 3) {
                    coupon_time_text.setVisibility(View.VISIBLE);
                    coupon_time_text.setText("分钟");
                    coupon_time.setText(String.valueOf(rowsBean.coupon.couponRule.deductionTime));
                } else if (type == 4) {
                    coupon_time_text.setVisibility(View.VISIBLE);
                    coupon_time_text.setText("折");
                    coupon_time.setText(String.valueOf(rowsBean.coupon.couponRule.discount / 10));
                }
                item_coupon_message.setText(rowsBean.coupon.name);
                use_message.setText(rowsBean.coupon.desc);
                due_time.setText(rowsBean.coupon.startTime + "~" + rowsBean.coupon.endTime);
                be_overdue_time.setText(TimeUtil.getTimeDay(rowsBean.coupon.startTime, rowsBean.coupon.endTime) + "天过期");

            }
        };
        recyclerAdapter.setDatas(couponRows);
        couponNotuseData.setAdapter(recyclerAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
